package com.miyake.server.servlet;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.miyake.client.datamanage.Data;
import com.miyake.client.datamanage.PlotData;
import com.miyake.server.DsData;
import com.miyake.server.DsPlot;
import com.miyake.server.dsaccess.NodeManager;

public class StoreXML {

	public StoreXML(BufferedInputStream bis, String key, String name) {
		try {
			DsData data = new DsData();
			data.setParsed(true);
			
			byte[] b = new StoreOriginal(data, bis, key, name).getByte();
			
			DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dbfactory.newDocumentBuilder();
			Document doc = builder.parse(new ByteArrayInputStream(b));

			Element root = doc.getDocumentElement();

			String srcFile = ((Element)root.getElementsByTagName("Name").item(0)).getTextContent();
			String columns = ((Element)root.getElementsByTagName("Columns").item(0)).getTextContent();
			String rows = ((Element)root.getElementsByTagName("Rows").item(0)).getTextContent();
			String titleRow = ((Element)root.getElementsByTagName("TitleRow").item(0)).getTextContent();

			// Data
			NodeList parameter = root.getElementsByTagName("Data");
			
			data.setTitleRow(0);
			data.setType(PlotData.DATA_Y);
			data.setXmin(0.0);
			data.setXmax(Double.valueOf(rows));
//			for (int i = 0; i < parameter.getLength(); i++) {			  
				NodeList items = parameter.item(0).getChildNodes();
				String unit =((Element)items).getAttribute("Unit");

				List<String> rd = new LinkedList<String>();
				
				for (int j = 0; j < items.getLength(); j++) {
					String d  = ((Element) items.item(j)).getTextContent();
					rd.add(d);
				}
				data.addData(rd, "");
				data.setSourceType(Data.SourceTypeFile);
				String nodeKey = NodeManager.getInstance().createData(name, key, data, -1);
//			}	
			
			// Chart
			NodeList chart = root.getElementsByTagName("Chart");
			if (chart.getLength() > 0) {
				NodeList chartElement = root.getElementsByTagName("ChartElement");
				
				for (int i = 0; i < chartElement.getLength(); i++) {			  
					NodeList chartItem = chartElement.item(i).getChildNodes();
					String chartName =((Element)chartItem).getAttribute("Name");
					String XAxis = ((Element)root.getElementsByTagName("XAxis").item(0)).getTextContent();
					String YAxis = ((Element)root.getElementsByTagName("YAxis").item(0)).getTextContent();
					
					NodeManager.getInstance().createPlot(chartName, nodeKey, getAutoPlot(nodeKey, Integer.valueOf(XAxis), Integer.valueOf(YAxis)), -1); 
				}
			}

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DsPlot getAutoPlot(String key, Integer xaxis, Integer yaxis) {
		DsData data = NodeManager.getInstance().getData(key);
		final DsPlot plot = new DsPlot();
		double min = Double.MAX_VALUE;
		double max = Double.MIN_VALUE;
		Double[] y = data.getDataDouble(0);
		
		plot.setXmax(data.getXmax());
		plot.setXmin(data.getXmin());

		plot.setxColumn(xaxis);
		plot.setyColumn(yaxis);
		
		for (int i = 0; i < y.length; i++) {
			if (y[i] > max) {
				max = y[i];
			}
			else if (y[i] < min) {
				min = y[i];
			}
		}
		plot.setYmax(max);
		plot.setYmin(min);
		
		plot.setType(PlotData.PLOT_LINE);
		return plot;
	}
	
}
